/**
 * type-util
 *
 * @author lihui
 */

// todo objectToString 对象转字符串
const objectToString = Object.prototype.toString
const toTypeString = value => objectToString.call(value)

const isObject = value => Object.prototype.toString.call(value) === '[object Object]'
// todo isPlainObject 判断是不是纯粹对象（数组不算是）
const isPlainObject = val => toTypeString(val) === '[object Object]'

const isArray = Array.isArray
const isMap = val => toTypeString(val) === '[object Map]'
const isSet = val => toTypeString(val) === '[object Set]'
const isDate = val => val instanceof Date
const isDate = val => val instanceof Date
const isFunction = val => typeof val === 'function'
const isString = val => typeof val === 'string'
const isSymbol = val => typeof val === 'symbol'
const isNumber = value => typeof value === 'number'
const isBoolean = value => typeof value === 'boolean'
const isEmpty = value => value === undefined || value === null
const isNotEmpty = value => value !== undefined && value !== null

// todo isPromise 判断是不是 Promise：是一个对象 & 有 then/catch 方法
const isPromise = val => {
  return isObject(val) && isFunction(val.then) && isFunction(val.catch)
}

const isError = value => Object.prototype.toString.call(value) === '[object Error]'
const isUrl = value => /https?:\/\/[^\s]*/.test(value)
const isEmail = value => {
  return /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(
    value,
  )
}

const TypeUtil = {
  isObject,
  isArray,
  isMap,
  isSet,
  isDate,
  isDate,
  isFunction,
  isString,
  isSymbol,
  isNumber,
  isBoolean,
  isEmpty,
  isNotEmpty,
  isPromise,
  isError,
  isUrl,
  isEmail,
}
